#!/bin/bash
# WeBASE调用脚本 - 调试版本
# 用于诊断WeBASE接口调用问题

echo "🔍 WeBASE零知识证明验证调试版"
echo "============================================"

# 1. 检查WeBASE服务状态
echo "1️⃣ 检查WeBASE服务状态..."
SERVICE_CHECK=$(curl -s --max-time 10 "http://localhost:5002/WeBASE-Front/monitor" 2>&1)
if [ $? -eq 0 ]; then
    echo "✅ WeBASE服务响应正常"
    echo "   响应内容: $SERVICE_CHECK"
else
    echo "❌ WeBASE服务不可达"
    echo "   错误信息: $SERVICE_CHECK"
    exit 1
fi

echo ""
echo "2️⃣ 执行合约调用..."

# 2. WeBASE合约调用 - 详细版
RESPONSE=$(curl -s -w "\nHTTP_CODE:%{http_code}\n" -X POST \
  -H "Content-Type: application/json" \
  --max-time 30 \
  --data '{
    "user": "0x81fb4b53cb3aa863b23a23d560a2296b5752e87b",
    "contractName": "DataAsset",
    "contractAddress": "0x745d4de0cf93b7d1db8dd8892daf05ac745766ce",
    "funcName": "verifyAssetZKPacked",
    "contractAbi": [
        {
            "inputs": [
                {
                    "name": "packedProof", 
                    "type": "bytes"
                }
            ],
            "name": "verifyAssetZKPacked", 
            "outputs": [
                {
                    "name": "", 
                    "type": "bool"
                }
            ],
            "stateMutability": "nonpayable",
            "type": "function"
        }
    ],
    "funcParam": ["0x29fbacf15e6a0706956d3befa88709ead0271cbd087a54f64d40b9df5b8b477f18af4adde43400ca990a1138eb485dea9a09dd51bb1782854c864df2717adfdc278d1b5bbcb180245e57de16a652311fbec35e47140f2d8fabc7e9b19a58f0b91201079f0bfc945ffd554b967e642a5bf1625f4bdb24f51572f6af0756a051f508b315d9053305bba5d84cabcde974d6746210fa77db07eb63a1015385a2155715d79e00a8ef602157e0ce80c867354bbdf330252b11291d1ad342fb765fbbf31f6ea55d3dfc745788f9449a10640518c1b30651c85fd82413506a41b8e2bcb124b3a33290b86aa804bbcd9139492b647719ff763ce1044cae427d5a55c8f16516e3deef28a13488c79c508ac1601561f42a01d9f41583af84afc225d6f78e29169ded792b419ecfe71b6d573803940a9a7ae0faf944be3381656673e67e49f81a523cb41d960a670896993a8f5a4a36309461572c8dc7e6e2dfdfbb1a4750382d67cc2cd6b0ac7016c095d630274b782d3a56e49194a679d6913aa302652b8725098528d09c6b6aa84b18618549504f757c2e12613fd5bea16fe21040e365242a0e311d813725efd8e70abccc2035e7db2e01988dab5d4b549f4fe435adac2416d025dcee13adadfe0e5f39ebc88216b7496bf9c777f4232be00ca6b5d45b6109288b73738691b0efafaac04e5e26bf0807585fc241d2d6e094a63677074d4a0c5437ac92c1100799dc267e97fc14e60024b32f96aab78f86dbe34ab09bd80b2e7f73104dbf240500889c5449704f6ecd5d42c70e08be2f96b59c97bade3930041cf1446869d221ff63ef3435febafc3f295c0e47e5d0c3201e2969573c4c10179d485c9a203cbc6b72f02f8ae8a8b0ef7b82b186e7e0a50ff9645a20e4ca8c040cd0fd9a193dbea748f557ddfbfcc9b0f8fa5dffaaa0e625212dbddbc122202e7ae08c486e5bbdee86747848b5b72a0d1a9d9768175764d614b95654e913ca1ccc07a8b89a80920079b90bc6eaac25cd10716f4b14bf2aafb69e271adf495a0e36d83293ccdfec065960d727a15c846605edf5196599ee81725fe350a772232ff0e6617bde5b26ca20173bd87c9fb0d49e6bcc0c20ba7ef0e939e0ecae7ea9"],
    "groupId": "group0",
    "contractPath": "/",
    "useCns": false,
    "cnsName": "",
    "version": ""
}' "http://localhost:5002/WeBASE-Front/trans/handle" 2>&1)

# 3. 分析响应结果
echo ""
echo "3️⃣ 分析响应结果..."
echo "============================================"

# 提取HTTP状态码
HTTP_CODE=$(echo "$RESPONSE" | grep "HTTP_CODE:" | cut -d':' -f2)
RESPONSE_BODY=$(echo "$RESPONSE" | sed '/HTTP_CODE:/d')

echo "📊 HTTP状态码: $HTTP_CODE"
echo ""
echo "📄 响应内容:"
echo "$RESPONSE_BODY"

# 4. 结果判断
echo ""
echo "4️⃣ 结果分析..."
echo "============================================"

if [ "$HTTP_CODE" = "200" ]; then
    echo "✅ HTTP请求成功 (200)"
    
    # 检查是否包含成功标识
    if echo "$RESPONSE_BODY" | grep -q '"status":"0x0"' || echo "$RESPONSE_BODY" | grep -q '"statusOK":true'; then
        echo "🎉 交易状态正常"
        
        if echo "$RESPONSE_BODY" | grep -q 'true'; then
            echo "🎯 零知识证明验证成功！ ✅"
        elif echo "$RESPONSE_BODY" | grep -q 'false'; then
            echo "❌ 零知识证明验证失败"
        else
            echo "⚠️ 未检测到明确的验证结果"
        fi
    else
        echo "❌ 交易执行出现问题"
    fi
else
    echo "❌ HTTP请求失败，状态码: $HTTP_CODE"
    echo "可能的原因："
    echo "  - WeBASE服务未运行"
    echo "  - 接口地址错误"
    echo "  - 参数格式错误"
    echo "  - 网络连接问题"
fi

echo ""
echo "============================================"
echo "🏁 调试完成" 